package com.example.arithmeticleetcode.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: arithmetic-leetcode
 * @description: 1436. 旅行终点站
 * @author: FangZhen
 * @create: 2020-05-06 10:36
 **/
public class Demo12 {

    public static void main(String[] args) {

    }

    public static String destCity(List<List<String>> paths) {
        Map<String, Integer> map = new HashMap<>();
        List<String> headList = new ArrayList<>();
        for (List<String> path : paths) {
            boolean flag = true;
            for (String s : path) {
                if (flag) {
                    headList.add(s);
                    flag = false;
                }
                map.put(s, map.getOrDefault(s, 0) + 1);
            }
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1 && !headList.contains(entry.getKey())) {
                return entry.getKey();
            }
        }
        return null;
    }

}
